import * as phaser from "phaser";
import {SourcePanel} from "@/widgets/SourcePanel.ts";
import {Source} from "@/sys/models.ts";

/**
 * 计分系统
 */
export class RatingSystem {
    static TARGET: number = 0
    static FISH: number = 1
    static NUMBER: number = 2

    private scene: phaser.Scene;
    private declare readonly sceneType: number;
    private sourcePanel: SourcePanel;
    // 总得分
    private source: number = 0;
    // 连续的正确错误计数
    private rightErrorList: number[] = [];
    // 反应成功/失败计数
    private reactionCounter: number[] = [];
    // 抑制成功/失败计数
    private restrainCounter: number[] = [];
    // 正确反应时,ms
    private rightCounter: number[] = [];

    constructor(scene: phaser.Scene, sceneType: number, sourcePanel: SourcePanel) {
        this.scene = scene;
        this.sceneType = sceneType;
        this.sourcePanel = sourcePanel;
    }

    getSource(): Source {
        return {
            source: this.source,
            reactionCount: this.reactionCounter, // 反应（成功失败）
            restrainCount: this.restrainCounter, // 抑制（成功失败）
            rightTime: this.rightCounter, // 时间
            attention: 0,
        };
    }

    /**
     * 反应正确时间 记录
     * @param time
     */
    onTimer(time: number) {
        this.rightCounter.push(time);
    }

    /**
     * 反应成功，指的是撞对目标或收集目标成功
     * @param type
     */
    reactionRight(type: number) {
        console.log('---- 反应成功 ----')
        if (type == RatingSystem.TARGET) {
            this.scene.sound.play('AudioRight');
        } else if (type == RatingSystem.FISH) {
            this.scene.sound.play('AudioFishRight');
        } else if (type == RatingSystem.NUMBER) {
            this.scene.sound.play('AudioNumberRight')
        }
        this.push(1);
        this.reactionCounter.push(1);
        this.updateSource(1);
        return true;
    }

    /**
     * 反应失败，指目标未收集并自行销毁
     */
    reactionError(autoDismiss: boolean = false) {
        console.log('---- 反应失败 ----', autoDismiss ? '自行销毁' : '撞错了')
        this.scene.sound.play('AudioError');
        this.push(0);
        this.reactionCounter.push(0);
        this.updateSource(-1);
    }

    /**
     * 抑制成功，指没有撞到或点掉干扰，且干扰自行销毁
     */
    restrainRight() {
        console.log('---- 抑制正确 ----')
        this.push(1);
        this.restrainCounter.push(1);
        this.updateSource(1);
    }

    /**
     * 抑制失败，指撞到或点掉干扰，则错误
     */
    restrainError() {
        console.log('---- 抑制错误 ----')
        this.push(0);
        this.scene.sound.play('AudioError');
        this.restrainCounter.push(0);
        this.updateSource(-1);
    }

    private push(num: number) {
        if (this.rightErrorList.length >= 5) {
            this.rightErrorList.shift();
        }
        this.rightErrorList.push(num);
    }

    // 检查连续出现指定次数的辅助方法
    private checkConsecutive(target: number, count: number) {
        let currentCount = 0;
        for (const num of this.rightErrorList) {
            if (num === target) {
                currentCount++;
                if (currentCount >= count) {
                    this.rightErrorList = [];
                    return true;
                }
            } else {
                currentCount = 0;
            }
        }
        return false;
    }

    private updateSource(val: number) {
        this.source += val;

        // 连续5次正确，额外+5分
        if (this.checkConsecutive(1, 5)) {
            this.source += 5;
            this.scene.sound.play('AudioAmazing')
        }
        // 连续3次错误，扣3分
        if (this.rightErrorList.length >= 3 && this.rightErrorList.slice(-3).every(i => i == 0)) {
            this.rightErrorList = [];
            this.source -= 3;
        }

        this.sourcePanel.updateSource(String(this.source));
    }
}